/*
 * spi_sw.cpp
 *
 *  Created on: 23.04.2013
 *      Author: Roma Jam
 */

#include "spi_sw.h"

//RJ adapted for PN532

Spi_sw_t Pn_Spi;

void Spi_sw_t::Init(){
	Mosi.SetupOut(PN_GPIO, PN_MOSI);
	Miso.SetupIn(PN_GPIO, PN_MISO);
	Cs.SetupOut(PN_NSS_IRQ_GPIO, PN_NSS);
	Cs.Hi();
	Sck.SetupOut(PN_GPIO, PN_SCK);
	Sck.Lo();
}

uint8_t Spi_sw_t::WriteReadByte(uint8_t AByte){
	uint8_t answ = 0;
	for(uint8_t i=0; i<8; i++){
		if(0x01 & AByte) Mosi.Hi();
		else Mosi.Lo();
		AByte >>= 1;
		Delay.us(1);
		Sck.Hi();
		Delay.us(1);
		answ >>= 1;
		if(Miso.isHi()) answ |= 0x80;
		Sck.Lo();
	}
	return answ;
}
